package com.jasonli.controller;

import com.jasonli.domain.Car;
import com.jasonli.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/cars")
public class CarController {
    @Autowired
    private CarService carService;

    @PostMapping
    public Result save(@RequestBody Car car) {
        boolean flag = carService.save(car);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    @PutMapping
    public Result update(@RequestBody Car car) {
        boolean flag = carService.update(car);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @DeleteMapping("/{chrid}")
    public Result delete(@PathVariable Integer chrid) {
        boolean flag = carService.delete(chrid);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    @GetMapping("/{cid}")
    public Result getByCid(@PathVariable Integer cid) {
        List<Car> info = carService.getByCid(cid);
        Integer code = info != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code, info);
    }
}
